package com.tanhua.server.controller;

import com.tanhua.common.pojo.Answers;
import com.tanhua.server.service.TestSoulService;
import com.tanhua.server.vo.QuestionnaireVo;
import com.tanhua.server.vo.TestResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("testSoul")
@Slf4j
public class TestSoulController {

    @Autowired
    private TestSoulService testSoulService;

    /**
     * 查看问卷列表
     *
     * @return
     */
    /**
     * 问卷列表
     *
     * @return
     */
    @GetMapping
    public ResponseEntity<List<QuestionnaireVo>> SoulQuestionnaire() {
        List<QuestionnaireVo> questionnaireVos = null;
        try {
            questionnaireVos = this.testSoulService.QuestionnaireLike();
            return ResponseEntity.ok(questionnaireVos);
        } catch (Exception e) {
            log.error("查看问卷失败", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 提交问卷
     *
     * @param answers
     */
    @PostMapping
    public String TestSoul(@RequestBody Map<String, List<Answers>> answers) {
        List<Answers> answersList = answers.get("answers");
        return this.testSoulService.testSoul(answersList);
    }

    /**
     * 测试结果
     *
     * @param id
     * @return
     */
    @GetMapping("report/{id}")
    public ResponseEntity<TestResultVo> TestResult(@PathVariable("id") Long id) {
        try {
            TestResultVo testResultVo = this.testSoulService.testResult(id);
            return ResponseEntity.ok(testResultVo);
        } catch (Exception e) {
            log.error("查看测试结果失败", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

}
